package com.sky.edumanageapi.comment.service.impl;

import com.sky.edumanageapi.base.exception.ServiceException;
import com.sky.edumanageapi.base.response.StatusCode;
import com.sky.edumanageapi.comment.dao.repository.CommentRepository;
import com.sky.edumanageapi.comment.pojo.dto.CommentParam;
import com.sky.edumanageapi.comment.pojo.entity.Comment;
import com.sky.edumanageapi.comment.pojo.vo.CommentVO;
import com.sky.edumanageapi.comment.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Override
    public List<CommentVO> getCommentListByCourseId(Integer courseId) {

        log.debug("获取课程评论,课程Id:{}", courseId);

        List<Comment> comments = commentRepository.getCommentListByCourseId(courseId);

        if (comments == null) {
            throw new ServiceException(StatusCode.COMMENTLIST_NULL);
        }

        List<CommentVO> commentVOS = new ArrayList<>();

        for (Comment comment : comments) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            commentVOS.add(commentVO);
        }


        return commentVOS;
    }

    @Override
    public void insert(CommentParam commentParam) {

        log.debug("发表评论传入参数:{}", commentParam);

        commentRepository.insert(commentParam);

    }

    @Override
    public List<CommentVO> getCommentListByUserId(Integer userId) {
        log.debug("获取用户评论,用户Id:{}", userId);

        List<Comment> comments = commentRepository.getCommentListByUserId(userId);

        if (comments == null) {
            throw new ServiceException(StatusCode.COMMENTLIST_NULL);
        }

        List<CommentVO> commentVOS = new ArrayList<>();

        for (Comment comment : comments) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            commentVOS.add(commentVO);
        }


        return commentVOS;
    }
}
